package cn.hg.jfsa.config;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.plugin.druid.IDruidStatViewAuth;

import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.model.JfsaAdmin;
import cn.hg.jfsa.model.JfsaResource;

public class JfsaDruidStatViewAuth implements IDruidStatViewAuth {

	private String actionKey;

	public JfsaDruidStatViewAuth (String actionKey) {
		this.actionKey = actionKey;
	}

	@Override
	public boolean isPermitted (HttpServletRequest request) {
		JfsaThreadLocal.setRequest(request);
		JfsaAdmin currAdmin = JfsaThreadLocal.getAdmin();

		if (null != currAdmin) {

			if (currAdmin.getId() == 1) {
				return true;
			}

			Map<String, JfsaResource> actionMap = JfsaThreadLocal.getAdminAuthAction();
			JfsaResource action = actionMap.get(this.actionKey);

			if (action != null) {
				return true;
			}

			return actionMap.containsKey(this.actionKey);
		}

		return false;
	}

}
